package cn.techjc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.stereotype.Service;

import cn.techjc.dao.UsersMapper;
import cn.techjc.dao.UsersVoteInfoMapper;
import cn.techjc.domain.Users;
import cn.techjc.domain.UsersVoteInfo;
import cn.techjc.service.VotedService;

/**
 *
 */
@Service
public class VotedServiceImpl implements VotedService {
	
	@Autowired
	private UsersMapper usersMapper;
	
	@Autowired
	private UsersVoteInfoMapper usersVoteInfo;
	
	@Autowired 
	public RedisTemplate<String,String> redisTemplate;

	@Override
	public List<Users> userList() {
		 List<Users> results = usersMapper.userList();
		return results;
	}
	
	@Override
	public void voteAdd(UsersVoteInfo voteInfo) {
		usersVoteInfo.insert(voteInfo);
	}
	
	@Override
	public int voteLimit(String ip) {
     int count =usersVoteInfo.voteLimit(ip);
		return count;
	}
	
	@Override
	public void setUsersNumToRedis(String name) {
		redisTemplate.opsForZSet().incrementScore("name", name, 1);
	}

	@Override
	public List<Map<String,String>> getUsersNumFromRedis() {
		List<Map<String,String>> results = new ArrayList<Map<String,String>>();
//                 Set<TypedTuple<String>> sets = redisTemplate.opsForZSet().rangeWithScores("name", 0,-1);
                 Set<TypedTuple<String>> sets = redisTemplate.opsForZSet().reverseRangeWithScores("name", 0, -1);
                 	for (TypedTuple<String> typedTuple : sets) {
                 		Map<String,String>  params = new HashMap<String, String>();
                 		 params.put("name",typedTuple.getValue());
                 		 params.put("score", String.valueOf(typedTuple.getScore().intValue()));
                 		 results.add(params);
					} 
		return results;
	}
    
    @Override
	public Users selectIdByName(String userName) {
    	Users user =usersMapper.selectIdByName(userName);
		return user;
	}

	@Override
	public List votedResult(int configId, String configName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> getVotesByOpenId(String openId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int insertVote(String openId, String id) {
		// TODO Auto-generated method stub
		return 0;
	}

}
